<?php
namespace backend\models;

use yii\base\Model;
use common\models\AdminModel;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $nickname;
    public $password;
	public $role_id;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['nickname', 'trim'],
            ['nickname', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\AdminModel', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 50],
            ['nickname', 'string','max' => 50],


            ['password', 'required'],
            ['password', 'string', 'min' => 6],
            
            ['role_id', 'required'],
        ];
    }

    /**
     * Signs user up.
     *
     * @return AdminModel|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new AdminModel();
        $user->username = $this->username;
        $user->nickname = $this->nickname;
        $user->role_id = $this->role_id;
        $user->setPassword($this->password);
        $user->generateAuthKey();

        $rs = $user->save();
            return $rs ? $user : null;
    }
}
